const PREFIX = "CACHE_MODELS_LOCAL"

export namespace Local {
    //
    export const set = (key:string,data:any,timeout?:number,call?:(d:any) => void) => {
        const d = get(key,true);
        if(d && d.handle){
            clearTimeout(d.handle)
        }
        const t:any = {time:new Date().getTime(),data,hadnle:null}

        if(timeout){
            t.handle = setTimeout(() => {
                call?.(data)
            },timeout)
        }
        //
        localStorage.setItem(key,JSON.stringify(t))
    }
    // 获取数据
    export const get = (key:string,isSource = false) => {
        const t = localStorage.getItem(key)
        if(!t){return undefined}
        const json = JSON.parse(t);
        return isSource ? json : json.data;
    }

    // 
    export const has = (key:string) => {
        return get(key) == undefined;
    }
}